package com.socket2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ServiceThreada implements Runnable {

	// 定义当前线程处理的Socket
	Socket s = null;
	// 该线程所处理的Socket所对应的输入流
	BufferedReader reader = null;
	BufferedWriter writer = null;

	public ServiceThreada(Socket s) {
		this.s = s;
		try {
			reader = new BufferedReader(new InputStreamReader(
					s.getInputStream()));
			writer = new BufferedWriter(new OutputStreamWriter(
					s.getOutputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		// // 写
		// try {
		// for (int i = 0; i < 5; i++) {
		// writer.write("==" + i);
		// writer.flush();
		// Thread.sleep(500);
		// }
		//
		// } catch (IOException | InterruptedException e1) {
		// e1.printStackTrace();
		// }

		while (true) {
			try {
				if (reader != null) {
					char[] buf = new char[2048];
					int length = reader.read(buf);
					System.out.println("the length is " + length);
					System.out.println("===" + new String(buf));
				} else {
					System.out.println("reader==null");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}


}